home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / dev / misc / gms_dev.lha / GMS / Source / C / 3DObjects / Torus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-09  |  3.9 KB  |  161 lines

  1. /*
  2. ** Name:      Spinning Torus
  3. ** Version:   1.0
  4. ** Author:    Paul Manias
  5. ** Copyright: DreamWorld Productions (c) 1997
  6. ** SAS/C:     sc Torus.c
  7. **
  8. ** Doc:       This is a demo of a spinning torus consisting entirely of dots.
  9. **            The object is pre-calculated then rotated in real time for speed,
  10. **            although things could be faster than this.
  11. **
  12. */
  13.  
  14. #include <proto/games.h>
  15. #include <math.h>
  16.  
  17. extern struct GMSBase *GMSBase;
  18. APTR   PREFSNAME = DEFAULT;
  19.  
  20. struct GameScreen *screen;
  21.  
  22. void Demo(void);
  23.  
  24. struct DotPixel { double X,Y,Z; };
  25.  
  26. #define AMTCOLOURS 32
  27.  
  28. ULONG palette[AMTCOLOURS] = {
  29.   0x000000,0x101010,0x171717,0x202020,0x272727,0x303030,0x373737,0x404040,
  30.   0x474747,0x505050,0x575757,0x606060,0x676767,0x707070,0x777777,0x808080,
  31.   0x878787,0x909090,0x979797,0xa0a0a0,0xa7a7a7,0xb0b0b0,0xb7b7b7,0xc0c0c0,
  32.   0xc7c7c7,0xd0d0d0,0xd7d7d7,0xe0e0e0,0xe0e0e0,0xf0f0f0,0xf7f7f7,0xffffff
  33. };
  34.  
  35. /***********************************************************************************/
  36.  
  37. void main(void)
  38. {
  39.   if (screen = AddScreenTags(TAGS_GAMESCREEN,NULL,
  40.      GSA_AmtColours, AMTCOLOURS,
  41.      GSA_Palette,    palette,
  42.      GSA_Attrib,     DBLBUFFER,
  43.      TAGEND)) {
  44.  
  45.      ShowScreen(screen);
  46.  
  47.      Demo();
  48.  
  49.   DeleteScreen(screen);
  50.   }
  51. }
  52.  
  53. /************************************************************************************
  54. ** Longtitude deterimines the position of the dot on the horizontal axis.
  55. ** Latitude determines the position of the dot on the vertical axis.
  56. */
  57.  
  58. #define AMTDOTS 1000       /* The amount of dots in the object. */
  59. #define MAXZ    1          /* -1 < Z < +1 */
  60.  
  61. void Demo(void)
  62. {
  63.   struct DotPixel *object;
  64.   WORD   i;
  65.   WORD   offsetx = (screen->ScrWidth/2);
  66.   WORD   offsety = (screen->ScrHeight/2);
  67.   double temp;
  68.   double angle=0;
  69.   ULONG  colour;
  70.   double Z2,X2,Y2;
  71.   ULONG  jport1;
  72.   LONG   scale=32;
  73.   UWORD  anglex=0,angley=0,anglez=0;
  74.   double u,v;
  75.   double *sine;       /* Pointer to our sine table */
  76.   double *cosine;     /* Pointer to our cosine table */
  77.  
  78.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_ANY);
  79.   sine   = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  80.   cosine = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  81.  
  82.   /* First calculate the X, Y and Z coordinates of our object */
  83.  
  84.   for (i=0; i<AMTDOTS; i++) {
  85.     u = ((double)FastRandom(6283))/1000;
  86.     v = ((double)FastRandom(6283))/1000;
  87.     object[i].X  = cos(u)*(2+cos(v));
  88.     object[i].Y  = sin(u)*(2+sin(v));
  89.     object[i].Z  = sin(v);
  90.   }
  91.  
  92.   /* Now generate our cosine and sinus tables */
  93.  
  94.   for (i=0; i<360; i++) {
  95.     cosine[i] = cos(angle);
  96.     sine[i]   = sin(angle);
  97.     angle    += 0.25;
  98.   }
  99.  
  100.   /* Go into our main loop */
  101.  
  102.   InitJoyPorts();
  103.  
  104.   do
  105.   {
  106.     jport1 = ReadJoyPort(JPORT1,JT_ZBXY);
  107.     scale += GetY(jport1);
  108.     if (scale < 1) scale = 1;
  109.     if (scale > 100) scale = 100;
  110.  
  111.     ClearBitmap(screen->Bitmap);
  112.  
  113.     for (i=0; i<AMTDOTS; i++) {
  114.  
  115.       X2 = object[i].X;
  116.       Y2 = object[i].Y;
  117.       Z2 = object[i].Z;
  118.  
  119.       /* Rotate the X axis */
  120.  
  121.       temp = Z2;
  122.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  123.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  124.  
  125.       /* Rotate the Y axis */
  126.  
  127.       temp = Z2;
  128.       Z2 = Z2*cosine[angley] - X2*sine[angley];
  129.       X2 = X2*cosine[angley] + temp*sine[angley];
  130.  
  131.       /* Rotate the Z axis */
  132.  
  133. //      temp = X2;
  134. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  135. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  136.  
  137.       /* Calculate colour based on Z position (-1 < Z < +1) */
  138.  
  139.       colour = (((Z2+MAXZ)/MAXZ)*screen->AmtColours)/2;
  140.  
  141.       /* Finally scale the (x,y) coordinates to enlarge or shrink the object */
  142.  
  143.       X2 *= scale;
  144.       Y2 *= scale;
  145.  
  146.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  147.     }
  148.     anglex++; if (anglex >= 360) anglex = 0;
  149.     angley++; if (angley >= 360) angley = 0;
  150.     anglez++; if (anglez >= 360) anglez = 0;
  151.  
  152.     WaitVBL();
  153.     SwapBuffers(screen);
  154.   } while(!(jport1 & MB_LMB));
  155.  
  156.   FreeMemBlock(object);
  157.   FreeMemBlock(sine);
  158.   FreeMemBlock(cosine);
  159. }
  160.  
  161.